// 文件上传模块

const express = require('express')

const router = express.Router()
const path = require('path')
const multer = require('multer')

// 上传文件的处理
const upload = multer({
    dest: path.join(__dirname, '../uploads/'),
    fileFilter(req, file, cb) {
        // 解决中文乱码问题
        file.originalname = Buffer.from(file.originalname, "latin1").toString("utf8");
        // 只允许上传png和jpg
        if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
            cb(null, true)
        } else {
            cb(null, false)
        }
    }
})

const uploadHandler = require('../router_handler/upload')

// 上传图片 标识字段名为img
router.post('/img', upload.single('img'), uploadHandler.uploadImg)

module.exports = router